package com.dm0858.bianmin.mine.tools;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Point;
import android.os.Build;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;

/**
 * @description 屏幕工具类
 * @author 迪迪丶Sir
 * @e-mail asd969709719@qq.com
 * @since 2020-03-17
 **/
public class ScreenUtils {

    private ScreenUtils() {
        throw new UnsupportedOperationException("u can't instantiate me");
    }

    /**
     * @description 获取屏幕宽度
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-17
     **/
    public static int getScreenWidth() {
        WindowManager manager = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
        if (manager == null) return -1;
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) manager.getDefaultDisplay().getRealSize(point);
        else  manager.getDefaultDisplay().getSize(point);
        return point.x;
    }

    /**
     * @description 获取屏幕高度
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-17
     **/
    public static int getScreenHeight() {
        WindowManager manager = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
        if (manager == null) return -1;
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) manager.getDefaultDisplay().getRealSize(point);
        else manager.getDefaultDisplay().getSize(point);
        return point.y;
    }

    /**
     * @description 获取App的屏幕宽度
     * @param  
     * @return 
     * @author 迪迪丶Sir
     * @since 2020-03-17
     **/
    public static int getAppScreenWidth() {
        WindowManager manager = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
        if (manager == null) return -1;
        Point point = new Point();
        manager.getDefaultDisplay().getSize(point);
        return point.x;
    }
    
    /**
     * @description 获取App的屏幕高度
     * @param  
     * @return 
     * @author 迪迪丶Sir
     * @since 2020-03-17
     **/
    public static int getAppScreenHeight() {
        WindowManager manager = (WindowManager) Utils.getApp().getSystemService(Context.WINDOW_SERVICE);
        if (manager == null) return -1;
        Point point = new Point();
        manager.getDefaultDisplay().getSize(point);
        return point.y;
    }

    /**
     * @description 获取屏幕缩放密度
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-17
     **/
    public static float getScreenScaleDensity() {
        return Utils.getApp().getResources().getDisplayMetrics().scaledDensity;
    }

    /**
     * @description 获取屏幕密度
     * @param  
     * @return 
     * @author 迪迪丶Sir
     * @since 2020-03-17
     **/
    public static float getScreenDensity() {
        return Utils.getApp().getResources().getDisplayMetrics().density;
    }

    /**
     * @description 获取屏幕密度dpi
     * @param  
     * @return 
     * @author 迪迪丶Sir
     * @since 2020-03-17
     **/
    public static int getScreenDensityDpi() {
        return Utils.getApp().getResources().getDisplayMetrics().densityDpi;
    }



    /**
     * @description 设置全屏
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-17
     **/
    public static void setFullScreen(@NonNull final Activity activity) {
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * @description 取消全屏
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-17
     **/
    public static void setNonFullScreen(@NonNull final Activity activity) {
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * @description activity在全屏跟非全屏切换
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-17
     **/
    public static void toggleFullScreen(@NonNull final Activity activity) {
        boolean isFullScreen = isFullScreen(activity);
        Window window = activity.getWindow();
        if (isFullScreen) window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        else window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * @description 判断是否全屏
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-03-17
     **/
    public static boolean isFullScreen(@NonNull final Activity activity) {
        int fullScreenFlag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        return (activity.getWindow().getAttributes().flags & fullScreenFlag) == fullScreenFlag;
    }

    /**
     *
     * @description 保持屏幕常亮
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-04-09
     **/
    public static void keepScreenOn(Activity activity){
        //开启屏幕常亮
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    /**
     *
     * @description 关闭屏幕常亮
     * @author 迪迪丶Sir
     * @e-mail asd969709719@qq.com
     * @since 2020-04-09
     **/
    public static void keepScreenOff(Activity activity){
        //关闭屏幕常亮
        activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    /**
     * @description 禁止截屏
     * @param
     * @return
     * @author 迪迪丶Sir
     * @since 2020-04-24
     **/
    public static void BanScreenshots(Activity activity) {
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_SECURE);
    }

}
